package com.ljh.mall.mallseckill.controller;

import com.alibaba.fastjson.JSON;
import com.ljh.common.utils.R;
import com.ljh.mall.mallseckill.dto.SeckillSkuRedisDto;
import com.ljh.mall.mallseckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：刘剑鸿
 * @date ：Created in 2023/5/21 0:39
 * @description：
 * @modified By：
 * @version: $
 */
@Controller
@RequestMapping("/seckill")
public class SeckillController {
    @Autowired
    SeckillService seckillService;

    @GetMapping("/currentSeckillSessionSkus")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @ResponseBody
    public R getCurrentSeckillSessionSkus(){
        List<SeckillSkuRedisDto> currentSeckillSkus = seckillService.getCurrentSeckillSkus();
        System.out.println("查询秒杀信息"+JSON.toJSONString(currentSeckillSkus));
        return R.ok().put("data", JSON.toJSONString(currentSeckillSkus));
    }


    @GetMapping("/seckillSessionBySkuId")
    @ResponseBody
    public R getSeckillSessionBySkuId(@RequestParam("skuId") Long skuId){
        SeckillSkuRedisDto dto = seckillService.getSeckillSessionBySkuId(skuId);
        return  R.ok().put("data",JSON.toJSONString(dto));
    }

    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("code") String code,
                          @RequestParam("num") Integer num,
                          Model model){
        System.out.println("进行kill");
        String orderSN = seckillService.kill(killId,code,num);
        model.addAttribute("orderSn",orderSN);
        return "success";
    }
}
